package com.ruoyi.merchant.service.impl;

import com.ruoyi.merchant.domain.*;
import com.ruoyi.merchant.dto.AbilityassessmentReportDto;
import com.ruoyi.merchant.mapper.AbilityassessmentBaseMapper;
import com.ruoyi.merchant.mapper.AbilityassessmentReportMapper;
import com.ruoyi.merchant.service.*;
import com.ruoyi.merchant.util.AssessmentEnum;
import com.ruoyi.merchant.util.FeelcommEnum;
import com.ruoyi.merchant.util.MentaEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 能力评估-评估报告Service业务层处理
 * 
 * @author chengxin.ju
 * @date 2023-12-24
 */
@Service
public class AbilityassessmentReportServiceImpl implements IAbilityassessmentReportService 
{
    @Autowired
    private AbilityassessmentReportMapper abilityassessmentReportMapper;

    /**
     * 查询能力评估-评估报告
     * 
     * @param baseId 能力评估-评估报告主键
     * @return 能力评估-评估报告
     */
    @Override
    public AbilityassessmentReport selectAbilityassessmentReportByBaseId(String baseId)
    {
        return abilityassessmentReportMapper.selectAbilityassessmentReportByBaseId(baseId);
    }

    /**
     * 查询能力评估-评估报告列表
     * 
     * @param abilityassessmentReport 能力评估-评估报告
     * @return 能力评估-评估报告
     */
    @Override
    public List<AbilityassessmentReport> selectAbilityassessmentReportList(AbilityassessmentReport abilityassessmentReport)
    {
        return abilityassessmentReportMapper.selectAbilityassessmentReportList(abilityassessmentReport);
    }

    /**
     * 新增能力评估-评估报告
     * 
     * @param abilityassessmentReport 能力评估-评估报告
     * @return 结果
     */
    @Override
    public int insertAbilityassessmentReport(AbilityassessmentReport abilityassessmentReport)
    {
        return abilityassessmentReportMapper.insertAbilityassessmentReport(abilityassessmentReport);
    }

    /**
     * 修改能力评估-评估报告
     * 
     * @param abilityassessmentReport 能力评估-评估报告
     * @return 结果
     */
    @Override
    public int updateAbilityassessmentReport(AbilityassessmentReport abilityassessmentReport)
    {
        return abilityassessmentReportMapper.updateAbilityassessmentReport(abilityassessmentReport);
    }

    /**
     * 批量删除能力评估-评估报告
     * 
     * @param baseIds 需要删除的能力评估-评估报告主键
     * @return 结果
     */
    @Override
    public int deleteAbilityassessmentReportByBaseIds(String[] baseIds)
    {
        return abilityassessmentReportMapper.deleteAbilityassessmentReportByBaseIds(baseIds);
    }

    /**
     * 删除能力评估-评估报告信息
     * 
     * @param baseId 能力评估-评估报告主键
     * @return 结果
     */
    @Override
    public int deleteAbilityassessmentReportByBaseId(String baseId)
    {
        return abilityassessmentReportMapper.deleteAbilityassessmentReportByBaseId(baseId);
    }

    @Autowired
    private IAbilityassessmentBaseService abilityassessmentBaseService;
    @Autowired
    private IAbilityassessmentFeelcommService abilityassessmentFeelcommService;
    @Autowired
    private IAbilityassessmentDailylifeService abilityassessmentDailylifeService;
    @Autowired
    private IAbilityassessmentMentalityService abilityassessmentMentalityService;
    @Autowired
    private IAbilityassessmentSocialService abilityassessmentSocialService;
    /**
     * 评估报告初始化-初步结果
     *
     * @param abilityassessmentReport
     * @param aDto
     * @return void
     * @author zhaocaisen
     * @since 2020-06-05
     */
    private void dealPreReport(AbilityassessmentReport abilityassessmentReport, AbilityassessmentReportDto aDto) {
        //获取日常生活活动
        AbilityassessmentDailylife abilityassessmentDailylife = abilityassessmentDailylifeService.selectAbilityassessmentDailylifeByBaseId(abilityassessmentReport.getBaseId());
        if (abilityassessmentDailylife == null){
            return;
        }
        if (abilityassessmentDailylife != null && abilityassessmentDailylife.getDailylifeDegree() != null){
            aDto.setDailylifeDegree(ablitity(abilityassessmentDailylife.getDailylifeDegree()));
        }
        //获取精神状态
        AbilityassessmentMentality abilityassessmentMentality = abilityassessmentMentalityService.selectAbilityassessmentMentalityByBaseId(abilityassessmentReport.getBaseId());
        if (abilityassessmentMentality != null && abilityassessmentMentality.getMentalityDegree() != null){
            aDto.setMentalityDegree(ablitity(abilityassessmentMentality.getMentalityDegree()));
        }
        //获取感觉与沟通
        AbilityassessmentFeelcomm abilityassessmentFeelcomm = abilityassessmentFeelcommService.selectAbilityassessmentFeelcommByBaseId(abilityassessmentReport.getBaseId());
        if (abilityassessmentFeelcomm != null && abilityassessmentFeelcomm.getFeelcommdegree() != null){
            aDto.setFeelcommDegree(ablitity(abilityassessmentFeelcomm.getFeelcommdegree()));
        }
        //获取社会参与
        AbilityassessmentSocial abilityassessmentSocial = abilityassessmentSocialService.selectAbilityassessmentSocialByBaseId(abilityassessmentReport.getBaseId());
        if( abilityassessmentSocial != null && abilityassessmentSocial.getSocialdegree() !=null){
            aDto.setSocialDegree(ablitity(abilityassessmentSocial.getSocialdegree()));
        }
        if (abilityassessmentDailylife.getDailylifeDegree() == 0 && abilityassessmentMentality.getMentalityDegree() == 0 && abilityassessmentFeelcomm.getFeelcommdegree() == 0 &&
                (abilityassessmentSocial.getSocialdegree() == 0 || abilityassessmentSocial.getSocialdegree() == 1)) {
            aDto.setAssessPreDegree(ablitity(0));
        }
        if (((abilityassessmentDailylife.getDailylifeDegree() == 0 && (abilityassessmentMentality.getMentalityDegree() >= 1 || abilityassessmentFeelcomm.getFeelcommdegree() >= 1)) ||
                (abilityassessmentSocial.getSocialdegree() == 2)) ||
                (abilityassessmentDailylife.getDailylifeDegree() == 2 && (abilityassessmentMentality.getMentalityDegree() <= 1 || abilityassessmentFeelcomm.getFeelcommdegree() <= 1 || abilityassessmentSocial.getSocialdegree() <= 1))) {
            aDto.setAssessPreDegree(ablitity(1));
        }

        if ((abilityassessmentDailylife.getDailylifeDegree() == 1 && abilityassessmentMentality.getMentalityDegree() == 2 && abilityassessmentFeelcomm.getFeelcommdegree() == 2 && abilityassessmentSocial.getSocialdegree() == 2)
                || (abilityassessmentDailylife.getDailylifeDegree() == 1 && (abilityassessmentMentality.getMentalityDegree() == 3 || abilityassessmentFeelcomm.getFeelcommdegree() == 3 || abilityassessmentSocial.getSocialdegree() == 3))
                || (abilityassessmentDailylife.getDailylifeDegree() == 2 && (abilityassessmentMentality.getMentalityDegree() != 3 && abilityassessmentFeelcomm.getFeelcommdegree() != 3 && abilityassessmentSocial.getSocialdegree() != 3) && ((abilityassessmentMentality.getMentalityDegree() == 0 || abilityassessmentFeelcomm.getFeelcommdegree() == 0 || abilityassessmentSocial.getSocialdegree() == 0)))
        ) {
            aDto.setAssessPreDegree(ablitity(2));
        }

        if ((abilityassessmentDailylife.getDailylifeDegree() == 3) || (abilityassessmentDailylife.getDailylifeDegree() == 2 && abilityassessmentMentality.getMentalityDegree() == 2 && abilityassessmentSocial.getSocialdegree() == 2 && abilityassessmentFeelcomm.getFeelcommdegree() == 2) ||
                (abilityassessmentDailylife.getDailylifeDegree() == 2 && (abilityassessmentMentality.getMentalityDegree() == 3 || abilityassessmentFeelcomm.getFeelcommdegree() == 3 || abilityassessmentSocial.getSocialdegree() == 3))) {
            aDto.setAssessPreDegree(ablitity(3));
        }
    }


    private String ablitity(int degree) {
        if (degree == 0) {
            return "0-能力完好";
        } else if (degree == 1) {
            return "1-轻度失能";
        } else if (degree == 2) {
            return "2-中度失能";
        } else if (degree == 3) {
            return "3-重度失能";
        }
        return null;
    }

    @Autowired
    private ICustomerBaseService customerBaseService;

    /**
     * 评估报告初始化-最终结果
     *
     * @param abilityassessmentReport
     * @param aDto
     * @return void
     * @author zhaocaisen
     * @since 2020-06-05
     */
    private void dealFinalReport(AbilityassessmentReport abilityassessmentReport, AbilityassessmentReportDto aDto) throws Exception {
        //能力评估基本信息
        AbilityassessmentBase abilityassessmentBase = abilityassessmentBaseService.selectAbilityassessmentBaseByID(abilityassessmentReport.getBaseId());
        //获取感觉与沟通
        AbilityassessmentFeelcomm abilityassessmentFeelcomm = abilityassessmentFeelcommService.selectAbilityassessmentFeelcommByBaseId(abilityassessmentReport.getBaseId());
        //获取精神状态
        AbilityassessmentMentality abilityassessmentMentality = abilityassessmentMentalityService.selectAbilityassessmentMentalityByBaseId(abilityassessmentReport.getBaseId());

        aDto.setAssessFinDegree(aDto.getAssessPreDegree());

        //4、若初步等级确定为“3重度失能”，则不考虑上述1-3中各情况对最终等级的影响，等级不再提升
        if (aDto.getAssessPreDegree().contains("3")) {
            aDto.setAssessFinDegree("3-重度失能");
            return;
        }

        //3、处于昏迷状态者，直接评定为重度失能；
        if (abilityassessmentFeelcomm!=null
                && abilityassessmentFeelcomm.getFeellevel()!=null
                &&  abilityassessmentFeelcomm.getFeellevel() == FeelcommEnum.FEELLEVEL3.getCode()) {
            aDto.setAssessFinDegree("3-重度失能");
            return;
        }


        //1、有认知障碍/痴呆、精神疾病者，在原有能力级别上提高一个等级；
        if (abilityassessmentMentality.getCognitive() == MentaEnum.COGNITIVE2.getCode()
                || abilityassessmentBase.getAssessmentDementia() >= AssessmentEnum.ASSESSMENTDEMENTIA1.getCode()
                || abilityassessmentBase.getAssessmentMentalill() >= AssessmentEnum.ASSESSMENTMENTALILL1.getCode()) {
            if (aDto.getAssessPreDegree().contains("0")) {
                aDto.setAssessFinDegree("1-轻度失能");
            } else if (aDto.getAssessPreDegree().contains("1")) {
                aDto.setAssessFinDegree("2-中度失能");
            } else if (aDto.getAssessPreDegree().contains("2")) {
                aDto.setAssessFinDegree("3-重度失能");
            }
        }

        //2、近30天内发生过2次以上跌倒、噎食、自杀、走失者，在原有能力级别上提高一个等级；
        if (abilityassessmentBase.getAssessmentFall() >= AssessmentEnum.ASSESSMENTACCIDENT2.getCode()
                || abilityassessmentBase.getAssessmentLost() > AssessmentEnum.ASSESSMENTACCIDENT2.getCode()
                || abilityassessmentBase.getAssessmentChoking() >= AssessmentEnum.ASSESSMENTACCIDENT2.getCode()
                || abilityassessmentBase.getAssessmentKillself() >= AssessmentEnum.ASSESSMENTACCIDENT2.getCode()) {
            if (aDto.getAssessFinDegree().contains("0")) {
                aDto.setAssessFinDegree("1-轻度失能");
            } else if (aDto.getAssessPreDegree().contains("1")) {
                aDto.setAssessFinDegree("2-中度失能");
            } else if (aDto.getAssessPreDegree().contains("2")) {
                aDto.setAssessFinDegree("3-重度失能");
            }
        }
        //更新报告评估结果

        //更新评估结果
        abilityassessmentBase.setAssessmentResult(aDto.getAssessFinDegree());
        CustomerBase customerBaseInfo = new CustomerBase();
        customerBaseInfo.setId(abilityassessmentBase.getCustomerId());
        if (aDto.getAssessFinDegree().contains("0-能力完好")){
            customerBaseInfo.setAssessDegree("0");
        }else if (aDto.getAssessFinDegree().contains("1-轻度失能")){
            customerBaseInfo.setAssessDegree("1");
        }else if (aDto.getAssessFinDegree().contains("2-中度失能")){
            customerBaseInfo.setAssessDegree("2");
        }else if (aDto.getAssessFinDegree().contains("3-重度失能")){
            customerBaseInfo.setAssessDegree("3");
        }
        customerBaseService.updateCustomerBase(customerBaseInfo);
        abilityassessmentBaseService.updateAbilityassessmentBase(abilityassessmentBase);

    }

    @Override
    public AbilityassessmentReportDto initReport(AbilityassessmentReport abilityassessmentReport) throws Exception {
        AbilityassessmentBase base = abilityassessmentBaseService.selectAbilityassessmentBaseByID(abilityassessmentReport.getBaseId());

        AbilityassessmentReportDto aDto = new AbilityassessmentReportDto();
        dealPreReport(abilityassessmentReport, aDto);
        dealFinalReport(abilityassessmentReport, aDto);
        aDto.setCustomerName(base.getAssessmentUsername());
        aDto.setAssessNumber(base.getAssessmentNumber());
        aDto.setCard(base.getAssessmentIdNumber());
        aDto.setAssessDate(base.getAssessmentData());
        aDto.setBirthDate(base.getAssessmentBirthday());
        aDto.setCustomerPhone(base.getAssessmentCustomerPhone());
        aDto.setAbilityassessmentBase(base);

        AbilityassessmentFeelcomm abilityassessmentFeelcomm = abilityassessmentFeelcommService.selectAbilityassessmentFeelcommByBaseId(abilityassessmentReport.getBaseId());
        aDto.setAbilityassessmentFeelcomm(abilityassessmentFeelcomm);
        AbilityassessmentReport report = selectAbilityassessmentReportByBaseId(abilityassessmentReport.getBaseId());
        if (report == null) {
            return aDto;
        }
        aDto.setAssessor(report.getAssessor());
        aDto.setInformationProvider(report.getInformationProvider());
        return aDto;
    }
}
